//
//  ChangePasswordController.m
//  coba-iOS
//
//  Created by 郭四虎 on 15/12/15.
//  Copyright © 2015年 YouKu. All rights reserved.
//

#import "ChangePasswordController.h"
#import "LoginViewController.h"

@interface ChangePasswordController ()
@property (weak, nonatomic) IBOutlet UITextField *oldPwd;

@property (weak, nonatomic) IBOutlet UITextField *nowPwd;
@property (weak, nonatomic) IBOutlet UITextField *confirmPwd;



@end

@implementation ChangePasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)submitClick:(id)sender {
    
    if ([self validPwd:_oldPwd.text]&&[self validPwd:_nowPwd.text]&&[self validPwd:_confirmPwd.text]) {
        
        if (![_nowPwd.text isEqualToString:_confirmPwd.text]) {
            [self.view Message:@"两次新密码输入不一样" HiddenAfterDelay:1];
            return;
        }
        
        NSDictionary *parameter = @{@"login_id":[Myuser userid:@""],@"old_password":[MyMD5 md5:_oldPwd.text],@"new_password":[MyMD5 md5:_nowPwd.text]};
        
        [MyAFN POST:CHANGEPWD parameters:parameter block:^(id responseObject) {
           
            
            NSDictionary *response = responseObject;
            NSLog(@"%@",response);

            if ([[response objectForKey:@"status"]integerValue]) {
                
                [[NSUserDefaults standardUserDefaults]setObject:[MyMD5 md5:_nowPwd.text] forKey:@"userpwd"];
                [self performSelector:@selector(message) withObject:nil afterDelay:0.5];
                
                [self performSelector:@selector(backtoLogin) withObject:nil afterDelay:1.5];
            }else{
                
                [self.view Message:[response objectForKey:@"msg"] HiddenAfterDelay:1];
                
            }
        }error:^(NSError *error) {
            
        }fromController:self];
        
    }else{
        
        [self.view Message:@"请输入6-32位密码" HiddenAfterDelay:1];
    }
}

- (void)message
{
[self.view Message:@"修改密码成功" HiddenAfterDelay:1];
}

- (void)backtoLogin
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (BOOL)validPwd:(NSString *)pwd
{
    NSString *str = @"^\\d{6,23}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",str];
    BOOL ismatch = [pred evaluateWithObject:pwd];
    
    return ismatch;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
